package com.newviews.tcmip.web.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.newviews.tcmip.common.config.ConfigProperties;
import com.newviews.tcmip.common.constant.CommonConsts;
import com.newviews.tcmip.common.model.PageModel;
import com.newviews.tcmip.common.model.ResultModel;
import com.newviews.tcmip.common.utils.ExportExcelUtils;
import com.newviews.tcmip.common.utils.UUIDGenerator;
import com.newviews.tcmip.dao.EtcmDiseaseMapper;
import com.newviews.tcmip.dao.EtcmHerbMapper;
import com.newviews.tcmip.dao.EtcmIngrMapper;
import com.newviews.tcmip.dao.EtcmPxMapper;
import com.newviews.tcmip.pojo.po.EtcmDiseasePo;
import com.newviews.tcmip.pojo.po.EtcmHerbPo;
import com.newviews.tcmip.pojo.po.EtcmIngrPo;
import com.newviews.tcmip.pojo.po.EtcmPxPo;
import com.newviews.tcmip.pojo.vo.export.EtcmDiseaseExportReqVO;
import com.newviews.tcmip.pojo.vo.export.EtcmPxExportReqVO;
import com.newviews.tcmip.pojo.vo.index.EtcmIndexStatusticsRespVO;
import com.newviews.tcmip.service.inf.*;
import com.newviews.tcmip.web.vo.combin.HerbHerbPageReqVo;
import com.newviews.tcmip.web.vo.etcmherb.download.EtcmHerbExportReqVO;
import com.newviews.tcmip.web.vo.etcmingr.EtcmIngrExportReqVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @ClassName: EtcmIndexController
 * @Description:
 * @Author: lxc
 * @Date: 2024-02-29 13:37
 * @Version: 1.0
 **/
@RestController
@RequestMapping("/etcm/index")
@Api(tags = "首页数据")
public class EtcmIndexController {
    @Autowired
    private EtcmHerbServiceInf etcmHerbService;

    @Autowired
    private EtcmPxServiceInf etcmPxService;

    @Autowired
    private EtcmIngrServiceInf etcmIngrService;

    @Autowired
    private EtcmDiseaseServiceInf etcmDiseaseService;

    @Autowired
    private ConfigProperties configProperties;
    @Resource
    private EtcmDiseaseExServiceInf etcmDiseaseExServiceInf;

    @Resource
    private EtcmPxMapper etcmPxMapper;
    @Resource
    private EtcmDiseaseMapper etcmDiseaseMapper;
    @Resource
    private EtcmHerbMapper etcmHerbMapper;
    @Autowired
    private EtcmIngrMapper etcmIngrMapper;

    @GetMapping("/statistics")
    @ApiOperation("首页数据统计")
    public ResultModel<EtcmIndexStatusticsRespVO> getStatistics(){
        EtcmIndexStatusticsRespVO respVO=new EtcmIndexStatusticsRespVO();

        int pxCount = etcmPxMapper.indexPxCount();
        respVO.setPrescriptions(pxCount);

        int herbCount = etcmHerbMapper.indexHerbCount();
        respVO.setHerbs(herbCount);

        int ingrCount = etcmIngrMapper.indexIngrCount();
        respVO.setIngredients(ingrCount);

        int diseaseCount = etcmDiseaseMapper.indexDiseaseCount();
        respVO.setDiseases(diseaseCount);

        return new ResultModel<EtcmIndexStatusticsRespVO>().success(respVO);
    }

    @GetMapping("/searchTip")
    @ApiOperation("首页搜索提词")
    public ResultModel<List> searchTip(@RequestParam("langCode") String langCode,
                                       @RequestParam("type") String){

    }

    @GetMapping("/search")
    @ApiOperation("首页搜索")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "searchType",required = true, value = "搜索类型 1-处方 2-草药 3-成分 4-疾病", paramType = "query"),
            @ApiImplicitParam(name = "content", value = "搜索内容", paramType = "query"),
            @ApiImplicitParam(name = "prepositionContent", value = "前置搜索内容", paramType = "query"),
    })
    public ResultModel<PageModel<Map<String,String>>> search(@RequestParam String searchType,
                                                             @RequestParam String content,
                                                             @RequestParam String langCode){
        return etcmPxService.search(searchType, content,langCode);
    }

    @GetMapping("/statistics/px")
    @ApiOperation("首页数据统计处方")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "prescriptionType", value = "处方类型", paramType = "query"),
            @ApiImplicitParam(name = "dosageForm", value = "方剂组成", paramType = "query"),
            @ApiImplicitParam(name = "prepositionContent", value = "前置搜索内容", paramType = "query"),

            @ApiImplicitParam(name = "langCode",required = true, value = "语言模式", paramType = "query"),
            @ApiImplicitParam(name = "uiTypeCode", required = true,value = "ui分类code", paramType = "query"),


    })
    public ResultModel<EtcmIndexStatusticsRespVO> getStatisticsPx(@RequestParam(required = false) String prescriptionType,
                                                                  @RequestParam(required = false) String dosageForm,
                                                                  @RequestParam String prepositionContent,

                                                                  @RequestParam String langCode,
                                                                  @RequestParam String uiTypeCode){
        return etcmPxService.getStatisticsPx(prescriptionType, dosageForm, prepositionContent, langCode, uiTypeCode);
    }

    @GetMapping("/prescription")
    @ApiOperation("首页搜索处方")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "prescriptionType", value = "处方类型", paramType = "query"),
            @ApiImplicitParam(name = "dosageForm", value = "方剂组成", paramType = "query"),
            @ApiImplicitParam(name = "prepositionContent", value = "前置搜索内容", paramType = "query"),
            @ApiImplicitParam(name = "content", value = "搜索内容", paramType = "query"),
            @ApiImplicitParam(name = "langCode",required = true, value = "语言模式", paramType = "query"),
            @ApiImplicitParam(name = "uiTypeCode", required = true,value = "ui分类code", paramType = "query"),
            @ApiImplicitParam(name = "pageNo", value = "当前页码 整数类型 默认值1", paramType = "query", defaultValue = "1"),
            @ApiImplicitParam(name = "pageSize", value = "页容量 整数类型 默认值10", paramType = "query", defaultValue = "10"),
            @ApiImplicitParam(name = "sortType", value = "排序方式 asc-正序;desc-倒序; 默认值desc", paramType = "query", defaultValue = "desc"),

    })
    public ResultModel<PageModel<Map<String,String>>> searchPx(@RequestParam(required = false) String prescriptionType,
                                                               @RequestParam(required = false) String dosageForm,
                                                               @RequestParam String prepositionContent,
                                                               @RequestParam(required = false) String content,
                                                               @RequestParam String langCode,
                                                               @RequestParam String uiTypeCode,
                                                               @RequestParam(required = false, defaultValue = "1") Integer pageNo,
                                                               @RequestParam(required = false, defaultValue = "10") Integer pageSize,
                                                               @RequestParam(required = false, defaultValue = "desc") String sortType){
        return etcmPxService.getIndexPage(prescriptionType, dosageForm, prepositionContent, content, langCode, uiTypeCode, pageNo, pageSize, sortType);
    }

    @PostMapping("/px/download")
    @ApiOperation(value = "首页搜索处方下载")
    public void downloadPx(
            @ApiIgnore HttpServletResponse response,
            @RequestBody @Valid EtcmPxExportReqVO reqVO) {
        try {
            List<Map<String, String>> pxList = etcmPxService.getIndexPageDownload(reqVO.getPrescriptionType(), reqVO.getDosageForm(), reqVO.getPrepositionContent(),reqVO.getContent(), reqVO.getLangCode(), reqVO.getUiTypeCode(),reqVO.getSortType());
            etcmDiseaseExServiceInf.getDownload(response,reqVO.getUiTypeCode(),reqVO.getLangCode(),reqVO.getExportType(),pxList);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    @GetMapping("/statistics/disease")
    @ApiOperation("首页数据统计疾病")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "prepositionContent", value = "前置搜索内容", paramType = "query"),

            @ApiImplicitParam(name = "langCode",required = true, value = "语言模式", paramType = "query"),
            @ApiImplicitParam(name = "uiTypeCode", required = true,value = "ui分类code", paramType = "query"),


    })
    public ResultModel<EtcmIndexStatusticsRespVO> getStatisticsDisease(
            @RequestParam String prepositionContent,
            @RequestParam String langCode,
            @RequestParam String uiTypeCode,
            @RequestParam(required = false) String type){
        return etcmDiseaseService.getStatisticsDisease(prepositionContent, langCode, uiTypeCode,type);
    }

    @GetMapping("/disease")
    @ApiOperation("首页搜索疾病")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "prepositionContent", value = "前置搜索内容", paramType = "query"),
            @ApiImplicitParam(name = "content", value = "搜索内容", paramType = "query"),
            @ApiImplicitParam(name = "langCode",required = true, value = "语言模式", paramType = "query"),
            @ApiImplicitParam(name = "uiTypeCode", required = true,value = "ui分类code", paramType = "query"),
            @ApiImplicitParam(name = "pageNo", value = "当前页码 整数类型 默认值1", paramType = "query", defaultValue = "1"),
            @ApiImplicitParam(name = "pageSize", value = "页容量 整数类型 默认值10", paramType = "query", defaultValue = "10"),
            @ApiImplicitParam(name = "sortType", value = "排序方式 asc-正序;desc-倒序; 默认值desc", paramType = "query", defaultValue = "desc"),

    })
    public ResultModel<PageModel<Map<String,String>>> searchDisease(
                                                               @RequestParam String prepositionContent,
                                                               @RequestParam(required = false) String content,
                                                               @RequestParam String langCode,
                                                               @RequestParam String uiTypeCode,
                                                               @RequestParam(required = false) String type,
                                                               @RequestParam(required = false, defaultValue = "1") Integer pageNo,
                                                               @RequestParam(required = false, defaultValue = "10") Integer pageSize,
                                                               @RequestParam(required = false, defaultValue = "desc") String sortType){
        return etcmDiseaseService.getIndexPage( prepositionContent, content, langCode, uiTypeCode, pageNo, pageSize, sortType,type);
    }

    @PostMapping("/disease/download")
    @ApiOperation(value = "首页搜索疾病下载")
    public void getDiseaseDownload(
            @ApiIgnore HttpServletResponse response,
            @RequestBody @Valid EtcmDiseaseExportReqVO reqVO) {
        try {
            List<Map<String, String>> pxList = etcmDiseaseService.getDiseaseDownload(reqVO);
            etcmDiseaseExServiceInf.getDownload(response,reqVO.getUiTypeCode(),reqVO.getLangCode(),reqVO.getExportType(),pxList);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @ApiOperation(value = "首页数据统计中草药")
    @GetMapping("/statistics/herb")
    public ResultModel<EtcmIndexStatusticsRespVO> getStatisticsHerb(@RequestParam @NotBlank(message = "语言编码不能为空") String langCode,
                                                              @RequestParam @NotBlank(message = "ui编码不能为空") String uiTypeCode,
                                                              @RequestParam(required = false) String prepositionContent,
                                                              @RequestParam(required = false) String meridians,
                                                              @RequestParam(required = false) String flavor,
                                                              @RequestParam(required = false) String property){
        return etcmHerbService.getStatisticsHerb(langCode,uiTypeCode,meridians,flavor,property,prepositionContent);
    }

    @ApiOperation(value = "首页搜索中草药")
    @GetMapping("/herb/page")
    public ResultModel<PageModel<Map<String,String>>> getPage(@RequestParam @NotBlank(message = "语言编码不能为空") String langCode,
                                                              @RequestParam @NotBlank(message = "ui编码不能为空") String uiTypeCode,
                                                              @RequestParam(required = false, defaultValue = "1") Integer pageNo,
                                                              @RequestParam(required = false, defaultValue = "10") Integer pageSize,
                                                              @RequestParam(required = false) String content,
                                                              @RequestParam(required = false) String prepositionContent,
                                                              @RequestParam(required = false) String meridians,
                                                              @RequestParam(required = false) String flavor,
                                                              @RequestParam(required = false) String property,
                                                              @RequestParam(required = false, defaultValue = "desc") String sortType){
        return etcmHerbService.getIndexPage(langCode,uiTypeCode,pageNo, pageSize, content,meridians,flavor,property,sortType,prepositionContent);
    }

    @PostMapping("/herb/download")
    @ApiOperation(value = "首页搜索中草药下载")
    public void downloadHerb(
            @ApiIgnore HttpServletResponse response,
            @RequestBody @Valid EtcmHerbExportReqVO reqVO) {
        try {
            List<Map<String, String>> pxList = etcmHerbService.getHerbDownload(reqVO);
            etcmDiseaseExServiceInf.getDownload(response,reqVO.getUiTypeCode(),reqVO.getLangCode(),reqVO.getExportType(),pxList);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @GetMapping("/statistics/ingr")
    @ApiOperation("首页数据统计成分")
    public ResultModel<EtcmIndexStatusticsRespVO>getStatisticsIngr(
                                                             @RequestParam @NotBlank(message = "语言编码不能为空") String langCode,
                                                             @RequestParam @NotBlank(message = "ui编码不能为空") String uiTypeCode,


                                                             @RequestParam(required = false) String prepositionContent){

        return etcmIngrService.getStatisticsIngr(langCode,uiTypeCode,prepositionContent);
    }
    @GetMapping("/ingr/page")
    @ApiOperation("首页搜索成分")
    public ResultModel<PageModel<Map<String,String>>>getIndexPage(@RequestParam(required = false) String content,
                                                             @RequestParam @NotBlank(message = "语言编码不能为空") String langCode,
                                                             @RequestParam @NotBlank(message = "ui编码不能为空") String uiTypeCode,
                                                             @RequestParam(required = false, defaultValue = "1") Integer pageNo,
                                                             @RequestParam(required = false) String prepositionContent,
                                                             @RequestParam(required = false, defaultValue = "10") Integer pageSize,
                                                             @RequestParam(required = false, defaultValue = "desc") String sortType){

        return etcmIngrService.getIndexPage(langCode,uiTypeCode,pageNo, pageSize, content,sortType,prepositionContent);
    }

    @PostMapping("/ingr/download")
    @ApiOperation(value = "首页搜索成分下载")
    public void downloadIngr(
            @ApiIgnore HttpServletResponse response,
            @RequestBody @Valid EtcmIngrExportReqVO reqVO) {
        try {
            List<Map<String, String>> pxList = etcmIngrService.getIngrDownload(reqVO);
            etcmDiseaseExServiceInf.getDownload(response,reqVO.getUiTypeCode(),reqVO.getLangCode(),reqVO.getExportType(),pxList);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
